package control;

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import entities.tools.Form;
import entities.tools.Question;
import entities.users.FormSigner;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.ServletSecurity.TransportGuarantee;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.ejb.FormBeanInterface;
import util.ejb.FormFillingBeanInterface;
import util.ejb.FormSignerBeanInterface;

/**
 *
 * @author Witold
 */
@WebServlet(urlPatterns = {"/InitializeDatabase"})
@ServletSecurity(
@HttpConstraint(transportGuarantee = TransportGuarantee.CONFIDENTIAL,rolesAllowed = {"admin"}))
public class InitializeDatabaseServlet  extends AbstractServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {

            /**
             * uncomment following code, if you run your application for the
             * first time, on your server the code will create all needed by
             * program entities
             */
            Form tmpForm = formBean.getExistingForm(new Long(1));
            if (null == tmpForm) {
                int orderNumber=0;
                tmpForm = formBean.createNewForm();
                tmpForm.setName("ANKIETA SATYSFAKCJI ZE SPOTKANIA");
                Question question1 = formBean.createNewQuestion(tmpForm);
                question1.setQuestion("1. Na ile Doradca wyczerpał temat z którym Pani /Pan przyszedł?");
                question1.setOrderNumber(orderNumber++);
                formBean.updateQuestion(tmpForm,question1);
                Question question2 = formBean.createNewQuestion(tmpForm);
                question2.setQuestion("2. Na ile Doradca skupił się na Pani/Pana potrzebach diagnozując je i dobierając odpowiednie rozwiązanie?");
                question2.setOrderNumber(orderNumber++);
                formBean.updateQuestion(tmpForm,question2);
                Question question31 = formBean.createNewQuestion(tmpForm);
                question31.setQuestion("3. Na ile Doradca posiadał wiedzę i kompetencje niezbędne do działalności gospodarczej w dziedzinie Finansów?");
                question31.setOrderNumber(orderNumber++); 
                formBean.updateQuestion(tmpForm,question31);                
                Question question3 = formBean.createNewQuestion(tmpForm);
                question3.setQuestion("4. Na ile spotkanie z Doradcą Markiem Sandomierskim było warte Pani/Pana czasu?");
                question3.setOrderNumber(orderNumber++); 
                formBean.updateQuestion(tmpForm,question3);                
                Question question4 = formBean.createNewQuestion(tmpForm);
                question4.setQuestion("5. Na ile posiadanie Indywidualnego Doradcy Finansowego jest dla Pani/Pana ważne?");
                question4.setOrderNumber(orderNumber++); 
                formBean.updateQuestion(tmpForm,question4);                
                Question question5 = formBean.createNewQuestion(tmpForm);
                question5.setQuestion("6. Na ile Doradca Marek Sandomierski jest Profesjonalistą wartym polecenia?");
                question5.setOrderNumber(orderNumber++); 
                formBean.updateQuestion(tmpForm,question5);                
                Question question6 = formBean.createNewQuestion(tmpForm);
                question6.setQuestion("7. Na ile posiadanie Indywidualnego Doradcy Finansowego może być ważne dla Pani/Pana znajomych?");
                question6.setOrderNumber(orderNumber++); 
                formBean.updateQuestion(tmpForm,question6);                
                Question question7 = formBean.createNewQuestion(tmpForm);
                question7.setQuestion("");
                question7.setOrderNumber(orderNumber++); 
                formBean.updateQuestion(tmpForm,question7);                
                Question question8 = formBean.createNewQuestion(tmpForm);
                question8.setQuestion("");
                question8.setOrderNumber(orderNumber++); 
                formBean.updateQuestion(tmpForm,question8);                
                Question question9 = formBean.createNewQuestion(tmpForm);
                question9.setQuestion("");
                question9.setOrderNumber(orderNumber++); 
                formBean.updateQuestion(tmpForm,question9);                
                Question question10 = formBean.createNewQuestion(tmpForm);
                question10.setQuestion("");
                question10.setOrderNumber(orderNumber++);
                formBean.updateQuestion(tmpForm,question10);                
                Question question11 = formBean.createNewQuestion(tmpForm);
                question11.setQuestion("");
                question11.setOrderNumber(orderNumber++); 
                formBean.updateQuestion(tmpForm,question11);                
                formBean.updateForm(tmpForm);
            }
            FormSigner tmpFormSigner = formSignerBean.getFormSigner(new Long(1));
            if (null == tmpFormSigner) {
                tmpFormSigner = formSignerBean.createFormSigner();
            }

        }
    }
}
